package com.fastloan.safe.mgt.root.controller;

import com.fastloan.safe.core.perms.Licensing;
import com.fastloan.safe.mgt.root.model.CmDefFun;
import com.fastloan.safe.mgt.root.service.CmDefFunResService;
import com.fastloan.safe.mgt.root.service.CmDefFunService;
import com.fastloan.safe.mgt.root.service.CmDefResApiService;
import com.fastloan.safe.mgt.root.service.CmDefResMenuService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultTo;
import com.kawins.spring.valid.core.BeanValid;
import com.kawins.spring.valid.group.Group;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author rankai
 *         createTime 2017-10-2017/10/16 17:46
 */
@RestController
@RequestMapping("/api/web/cmDefFun")
public class CmDefFunController extends BaseController<CmDefFun> {

    @Autowired
    private CmDefFunService defFunService;

    @Autowired
    private CmDefFunResService defFunResService;

    @Autowired
    private CmDefResApiService defResApiService;

    @Autowired
    private CmDefResMenuService defResMenuService;

    @Override
    protected Logger buildLogger() {
        return LoggerFactory.getLogger(CmDefFunController.class);
    }

    /**
     * 功能分页列表
     *
     * @return
     */
    @RequestMapping("/pageList")
    @RequiresPermissions("all:/web/cmDefFun/pageList")
    @Licensing({"add#post:/web/cmDefFun", "update#put:/web/cmDefFun", "del#delete:/web/cmDefFun/cmDefFunId"})
    public ResultTo pageList() {
        return dataPage("defFunPageList");
    }

    /**
     * 获取增加或者修改页面数据
     *
     * @param defFunId
     * @return
     */
    @GetMapping("/param")
    @RequiresPermissions(value = {"post:/web/cmDefFun", "put:/web/cmDefFun"}, logical = Logical.OR)
    public ResultTo param(@RequestParam(required = false) String defFunId) {
        Map<String, Object> map = new HashMap<>();
        if (defFunId != null) {
            map.put("defFun", adaptService.selectByPrimaryKey(defFunId));
            map.put("defResApiList", defResApiService.selectDefApiResByFunId(defFunId));
            map.put("defResMenuList", defResMenuService.selectDefMenuResByFunId(defFunId));
        }
        return new ResultTo().setData(map);
    }

    /**
     * 获取所有API资源,带搜索
     *
     * @param value 可以是名称,或URL
     * @return
     */
    @GetMapping("/param/resApi")
    @RequiresPermissions(value = {"post:/web/cmDefFun", "put:/web/cmDefFun"}, logical = Logical.OR)
    public ResultTo paramForApi(@RequestParam(required = false) String value) {
        return new ResultTo().setData(defFunResService.paramForApi(value));
    }

    /**
     * 获取所有菜单资源,带搜索
     *
     * @param value 名称搜索
     * @return
     */
    @GetMapping("/param/resMenu")
    @RequiresPermissions(value = {"post:/web/cmDefFun", "put:/web/cmDefFun"}, logical = Logical.OR)
    public ResultTo paramForMenu(@RequestParam(required = false) String value) {
        return new ResultTo().setData(defFunResService.paramForMenu(value));
    }

    /**
     * 增加功能
     *
     * @return
     */
    @PostMapping
    @BeanValid
    @RequiresPermissions("post:/web/cmDefFun")
    public ResultTo insert(@Validated(Group.Insert.class) CmDefFun cmDefFun, BindingResult bindingResult,
                           String[] apiIds, String[] menuIds) {
        defFunService.insertForShort(cmDefFun, apiIds, menuIds);
        return new ResultTo();
    }

    /**
     * 更新功能
     *
     * @param cmDefFun
     * @param apiIds
     * @param menuIds
     * @return
     */
    @PutMapping
    @BeanValid
    @RequiresPermissions("put:/web/cmDefFun")
    public ResultTo update(@Validated(Group.Update.class) CmDefFun cmDefFun, BindingResult bindingResult,
                           String[] apiIds, String[] menuIds) {
        defFunService.updateForShort(cmDefFun, apiIds, menuIds);
        return new ResultTo();
    }

    /**
     * 删除功能
     *
     * @param cmDefFunId
     * @return
     */
    @DeleteMapping("/{cmDefFunId}")
    @RequiresPermissions("delete:/web/cmDefFun/cmDefFunId")
    public ResultTo delete(@PathVariable String cmDefFunId) {
        defFunService.deleteAll(cmDefFunId);
        return new ResultTo();
    }
}
